﻿using System;
using System.IO;
using System.Windows.Forms;
using loader.Windows;
using System.Runtime.InteropServices;
using System.Drawing;

namespace loader
{
    public partial class exeselect : Form
    {
        private string DFileSize(Int64 fsize)
        {
            double outp;
            if (fsize > 1073741824)
            {
                outp = (fsize / 1073741824);
                outp = Math.Round(outp, 3);
                return outp.ToString() + " Gib";
            }
            else if (fsize > 1048576)
            {
                outp = (fsize / 1048576);
                outp = Math.Round(outp, 3);
                return outp.ToString() + " Mib";
            }
            else if (fsize > 1024)
            {
                outp = (fsize / 1024);
                outp = Math.Round(outp, 3);
                return outp.ToString() + " Kib";
            }
            else return fsize.ToString() + " byte";
        }

        public exeselect(string [] items)
        {
            InitializeComponent();
            Icon FileIcon;
            ListViewItem it;
            int icount = 0;
            if (Program.localee.issupported)
            {
                this.Text = Program.localee["exeselect_title"];
                this.label1.Text = Program.localee["exeselect_description"];
                this.btn_ok.Text = Program.localee["btn_ok"];
                this.btn_cancel.Text = Program.localee["btn_cancel"];
            }
            this.exebox.Items.Clear();
            SHFILEINFO Fil = new SHFILEINFO();
            FileInfo Fi;
            foreach (string str in items)
            {
                Win32.SHGetFileInfo(str, 0, ref Fil, (uint)Marshal.SizeOf(Fil), Win32.SHGFI_LARGEICON | Win32.SHGFI_ICON);
                FileIcon = Icon.FromHandle(Fil.hIcon);
                this.ExeImages.Images.Add(FileIcon);
                Fi = new FileInfo(str);
                it = new ListViewItem(Path.GetFileName(str).ToLower());
                it.ImageIndex = icount++;
                it.SubItems[0].Text = Path.GetFileName(str).ToLower();
                it.SubItems.Add(this.DFileSize(Fi.Length));
                this.exebox.Items.Add(it);
            }
            this.exebox.LargeImageList = this.ExeImages;
        }

        public int selectedexe
        {
            get
            {
                return this.exebox.SelectedIndices[0];
            }
        }

        private void exebox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.exebox.SelectedItems.Count > 0) this.btn_ok.Enabled = true;
            else this.btn_ok.Enabled = false;     
        }

        private void exebox_DoubleClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}
